/****************************************************************************
**
** Copyright (C) 2010 Tieto Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Tieto Corporation (info.ostrava@tieto.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Tieto.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Tieto gives you certain additional
** rights.  These rights are described in the Tieto Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Tieto at info.ostrava@tieto.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include <math.h>
#include "etalonmodel.h"

EtalonModel::EtalonModel(QObject *parent) :
    FeatureModel(parent),
    etalonId(1)
{
    reset();
}

void EtalonModel::reset()
{
    clear();
    QStringList headerLabels; // TODO headers
    headerLabels <<
            tr("Name") <<
            tr("Image") <<
            tr("TODO") <<
            tr("range");
    setHorizontalHeaderLabels(headerLabels);
}

void EtalonModel::addEtalon()
{
    int rows = rowCount();
    QString name(tr("Etalon"));
    name += QString::number(etalonId++);
    setFeature(rows, EName, name);
}

void EtalonModel::removeEtalon(int row)
{
    removeRow(row);
}

void EtalonModel::updateEtalon(const QString &name, const Feature &feature)
{
    QStringList names = etalonNames();
    int row = names.indexOf(name);
    if (row > -1) {
        double value = feature.value;
        int type = feature.type;
        double radius = 0.0;
        QVariant variantValue = this->feature(row, type);
        QVariant variantRadius = this->feature(row, type + 1);
        if (variantValue.isValid()) {
            double etalonValue = qVariantValue<double>(variantValue);
            double etalonRadius = qVariantValue<double>(variantRadius);
            double max = etalonValue + etalonRadius;
            double min = etalonValue - etalonRadius;
            if (value >= min && value <= max) {
                return;
            }
            else if (value < min) {
                min = value;
            }
            else if(value > max) {
                max = value;
            }
            value = (min + max) / 2;
            radius = (max - min) / 2;
        }
        setFeature(row, type, value);
        setFeature(row, type + 1, radius);
    }
}

QString EtalonModel::recognize(const Features &features)
{
    QString name;
    QStringList names = etalonNames();
    for (int row = 0; row < rowCount(); row++) {
        if (recognizeEtalonFeatures(row, features)) {
            name = feature(row, EName).toString();
            break;
        }
    }
    return name;
}

QStringList EtalonModel::etalonNames()
{
    QStringList names;
    for (int row = 0; row < rowCount(); row++) {
        names << qVariantValue<QString>(feature(row, EName));
    }
    return names;
}

Qt::ItemFlags EtalonModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    if (index.column() == EName) { // TODO
        flags |= Qt::ItemIsEditable;
    }
    return flags;
}

bool EtalonModel::recognizeEtalonFeatures(int row, const Features &features)
{
    bool result = false;
    for (int i = 0; i < features.size(); i++) {
        Feature feature = features[i];
        if (!recognizeEtalonFeature(row, feature)) {
            result = false;
            break;
        }
        result = true;
    }
    return result;
}

bool EtalonModel::recognizeEtalonFeature(int row, const Feature &feature)
{
    bool result = false;
    double value = feature.value;
    int type = feature.type;
    QVariant variantValue = this->feature(row, type);
    QVariant variantRadius = this->feature(row, type + 1);
    if (variantValue.isValid()) {
        double etalonValue = qVariantValue<double>(variantValue);
        double etalonRadius = qVariantValue<double>(variantRadius);
        double max = etalonValue + etalonRadius;
        double min = etalonValue - etalonRadius;
        if (value >= min && value <= max) {
            result = true;
        }
    }
    return result;
}
